package com.sunfeng.n3;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * 测试线程状态
 *
 * @author sunfeng
 * @date 2021/03/17 13:26
 **/

@Slf4j(topic = "c.TestState")
public class TestState {
    public static void main(String[] args) {

        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                log.debug("running.......");
            }
        };

        Thread t2 = new Thread("t2") {
            @Override
            public void run() {
               while (true){

               }
            }
        };
        t2.start();

        Thread t3 = new Thread("t3") {
            @Override
            public void run() {
                log.debug("running.......");
            }
        };
        t3.start();

        Thread t4 = new Thread("t4") {
            @Override
            public void run() {
               synchronized (TestState.class){
                   try {
                       TimeUnit.SECONDS.sleep(1000000); //time_waiting
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
            }
        };

        t4.start();

        Thread t5 = new Thread("t5") {
            @Override
            public void run() {
                try {
                    t2.join();   //waiting
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t5.start();

        Thread t6 = new Thread("t6") {
            @Override
            public void run() {
                synchronized (TestState.class){
                    try {
                        TimeUnit.SECONDS.sleep(1000000); //time_waiting
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t6.start();
        /**
         * NEW
         */
        log.debug("t1的线程状态为：{}",t1.getState());
        /**
         * RUNNABLE
         */
        log.debug("t2的线程状态为：{}",t2.getState());
        /**
         * TERMINATED
         */
        log.debug("t3的线程状态为：{}",t3.getState());
        /**
         * TIMED_WAITING
         */
        log.debug("t4的线程状态为：{}",t4.getState());
        /**
         * WAITING
         */
        log.debug("t5的线程状态为：{}",t5.getState());
        /**
         * BLOCKED
         */
        log.debug("t6的线程状态为：{}",t6.getState());
    }
}
